package com.kuaiditu.assistant.ui.main;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.kuaiditu.assistant.R;
import com.kuaiditu.assistant.api.assistant_api.AssistantApi;
import com.kuaiditu.assistant.api.assistant_api.respone.TaskSquareRes;
import com.kuaiditu.assistant.base.BaseFragment;
import com.kuaiditu.assistant.entity.TaskSquareEn;
import com.kuaiditu.assistant.module.retrofit.ProgressSubscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class CompleteTaskFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.rv_complete_task)
    RecyclerView mRvCompleteTask;
    @BindView(R.id.swiper)
    SwipeRefreshLayout swiper;
    Unbinder unbinder;
    private String title = "已完成任务";


    private CompleteTaskAdapter mCompleteTaskAdapter;

//    private RecyclerView mRvCompleteTask;


    List<TaskSquareEn> mTasks = new ArrayList<>();

    AssistantApi mAssistantApi;
    int page = 1;
    private boolean isRefresh;

    @Override
    public void initInjector() {
        mAssistantApi = getApplicationComponent().getApiManager().getAssistantApi();

    }

    @Override
    public int initContentView() {
        return R.layout.fragment_complete_task;
    }

    @Override
    public void getBundle(Bundle bundle) {

    }

    @Override
    public void initUI(View v) {

        unbinder = ButterKnife.bind(this, v);

        mRvCompleteTask = (RecyclerView) v.findViewById(R.id.rv_complete_task);

        mRvCompleteTask.setLayoutManager(new LinearLayoutManager(mContext));
        mRvCompleteTask.setAdapter(mCompleteTaskAdapter = new CompleteTaskAdapter(mTasks));

        swiper.setOnRefreshListener(this);

        onRefresh();

    }

    @Override
    public void initData() {

    }


    private void fetchMyTask() {
        mAssistantApi.fetchPersonTask(page, AssistantApi.TYPE_TASK_END)
                .subscribe(new ProgressSubscriber<TaskSquareRes>(mContext) {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onNext(TaskSquareRes taskSquareRes) {
                        isRefresh = false;
                        mTasks.addAll(taskSquareRes.getListOrder());
                        mCompleteTaskAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinish() {
                        swiper.setRefreshing(false);
                    }
                });
    }


    @Override
    public String getTitle() {
        return title;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        swiper.setRefreshing(true);
        mTasks.clear();
        page = 1;
        fetchMyTask();
        isRefresh = true;
    }
}
